package com.h.heal.controller;

import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.User;
import com.h.heal.entity.Video;
import com.h.heal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserContorller {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(User user, HttpSession httpSession) {
        return userService.login(user, httpSession);
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(User user) {
        return userService.register(user);
    }


    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request,ModelAndView mv) {
        mv.setViewName("/admin/user/main");
        MyPageInfo myPageInfo = userService.findBySearch(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;

    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        userService.deleteByUser_uid(id);
        return "redirect:/user/main";
    }


    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv) {
        User user = userService.findById(id);
        mv.setViewName("/admin/user/edit");
        mv.addObject("user", user);
        return mv;
    }

    @RequestMapping("/doEdit")
    @ResponseBody
    public String doEdit(User user) {
        return userService.edit(user);
    }



    @RequestMapping("/user/openEdit/{id}")
    public ModelAndView userOpenEdit(@PathVariable("id") int id, ModelAndView mv) {
        User user = userService.findById(id);
        mv.setViewName("/user/user/edit");
        mv.addObject("user", user);
        return mv;
    }

    @RequestMapping("/user/doEdit")
    @ResponseBody
    public String userDoEdit(User user) {
        return userService.edit(user);
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();  //设置session失效
        return "redirect:/html/welcome/welcome.html";  //重定向到登录页面
    }

}
